"""
货物相关Pydantic模式
"""
from datetime import datetime
from typing import Optional, List
from decimal import Decimal
from pydantic import BaseModel, Field, validator

from .base import BaseSchema, TimestampSchema


class CargoCategoryBase(BaseModel):
    """货物分类基础模式"""
    name: str = Field(..., max_length=100, description="分类名称")
    code: str = Field(..., max_length=50, description="分类代码")
    description: Optional[str] = Field(None, description="分类描述")
    parent_id: Optional[int] = Field(None, description="父分类ID")
    level: int = Field(default=1, description="分类层级")
    sort_order: int = Field(default=0, description="排序")


class CargoCategoryCreate(CargoCategoryBase):
    """创建货物分类模式"""
    pass


class CargoCategoryUpdate(BaseModel):
    """更新货物分类模式"""
    name: Optional[str] = Field(None, max_length=100, description="分类名称")
    description: Optional[str] = Field(None, description="分类描述")
    sort_order: Optional[int] = Field(None, description="排序")


class CargoCategoryResponse(CargoCategoryBase, TimestampSchema):
    """货物分类响应模式"""
    id: int = Field(..., description="分类ID")
    children: Optional[List["CargoCategoryResponse"]] = Field(default=[], description="子分类")


class CargoBase(BaseModel):
    """货物基础模式"""
    name: str = Field(..., max_length=200, description="货物名称")
    code: str = Field(..., max_length=100, description="货物编码")
    barcode: Optional[str] = Field(None, max_length=100, description="条形码")
    qr_code: Optional[str] = Field(None, max_length=200, description="二维码")
    category_id: int = Field(..., description="货物分类ID")
    brand: Optional[str] = Field(None, max_length=100, description="品牌")
    model: Optional[str] = Field(None, max_length=100, description="型号")
    specification: Optional[str] = Field(None, description="规格说明")
    weight: Optional[Decimal] = Field(None, ge=0, description="重量(kg)")
    volume: Optional[Decimal] = Field(None, ge=0, description="体积(m³)")
    length: Optional[Decimal] = Field(None, ge=0, description="长度(cm)")
    width: Optional[Decimal] = Field(None, ge=0, description="宽度(cm)")
    height: Optional[Decimal] = Field(None, ge=0, description="高度(cm)")
    unit_price: Optional[Decimal] = Field(None, ge=0, description="单价")
    currency: str = Field(default="CNY", max_length=10, description="货币单位")
    warehouse_id: Optional[int] = Field(None, description="所在仓库ID")
    location: Optional[str] = Field(None, max_length=100, description="具体位置")
    description: Optional[str] = Field(None, description="货物描述")
    notes: Optional[str] = Field(None, description="备注")


class CargoCreate(CargoBase):
    """创建货物模式"""
    
    @validator('code')
    def validate_code(cls, v):
        if not v or len(v.strip()) == 0:
            raise ValueError('货物编码不能为空')
        return v.strip().upper()


class CargoUpdate(BaseModel):
    """更新货物模式"""
    name: Optional[str] = Field(None, max_length=200, description="货物名称")
    barcode: Optional[str] = Field(None, max_length=100, description="条形码")
    qr_code: Optional[str] = Field(None, max_length=200, description="二维码")
    category_id: Optional[int] = Field(None, description="货物分类ID")
    brand: Optional[str] = Field(None, max_length=100, description="品牌")
    model: Optional[str] = Field(None, max_length=100, description="型号")
    specification: Optional[str] = Field(None, description="规格说明")
    weight: Optional[Decimal] = Field(None, ge=0, description="重量(kg)")
    volume: Optional[Decimal] = Field(None, ge=0, description="体积(m³)")
    length: Optional[Decimal] = Field(None, ge=0, description="长度(cm)")
    width: Optional[Decimal] = Field(None, ge=0, description="宽度(cm)")
    height: Optional[Decimal] = Field(None, ge=0, description="高度(cm)")
    unit_price: Optional[Decimal] = Field(None, ge=0, description="单价")
    currency: Optional[str] = Field(None, max_length=10, description="货币单位")
    warehouse_id: Optional[int] = Field(None, description="所在仓库ID")
    location: Optional[str] = Field(None, max_length=100, description="具体位置")
    description: Optional[str] = Field(None, description="货物描述")
    notes: Optional[str] = Field(None, description="备注")
    status: Optional[str] = Field(None, description="状态")


class CargoResponse(CargoBase, TimestampSchema):
    """货物响应模式"""
    id: int = Field(..., description="货物ID")
    status: str = Field(..., description="状态")
    category: Optional[CargoCategoryResponse] = Field(None, description="货物分类")


class CargoImageBase(BaseModel):
    """货物图片基础模式"""
    image_url: str = Field(..., max_length=500, description="图片URL")
    image_type: str = Field(default="product", max_length=20, description="图片类型")
    description: Optional[str] = Field(None, max_length=200, description="图片描述")
    sort_order: int = Field(default=0, description="排序")


class CargoImageCreate(CargoImageBase):
    """创建货物图片模式"""
    cargo_id: int = Field(..., description="货物ID")


class CargoImageResponse(CargoImageBase, TimestampSchema):
    """货物图片响应模式"""
    id: int = Field(..., description="图片ID")
    cargo_id: int = Field(..., description="货物ID")


class CargoAttributeBase(BaseModel):
    """货物属性基础模式"""
    attribute_name: str = Field(..., max_length=100, description="属性名称")
    attribute_value: str = Field(..., description="属性值")
    attribute_type: str = Field(default="text", max_length=20, description="属性类型")


class CargoAttributeCreate(CargoAttributeBase):
    """创建货物属性模式"""
    cargo_id: int = Field(..., description="货物ID")


class CargoAttributeResponse(CargoAttributeBase, TimestampSchema):
    """货物属性响应模式"""
    id: int = Field(..., description="属性ID")
    cargo_id: int = Field(..., description="货物ID")


class CargoListResponse(BaseModel):
    """货物列表响应模式"""
    items: List[CargoResponse] = Field(..., description="货物列表")
    total: int = Field(..., description="总数量")
    page: int = Field(..., description="当前页码")
    size: int = Field(..., description="每页数量")
    pages: int = Field(..., description="总页数")


# 更新前向引用
CargoCategoryResponse.model_rebuild()
